#pragma once

#include <unordered_map>
#include "PrimitiveDirection.h"

using namespace std;

class PrimitivesCounter
{
public:
	PrimitivesCounter(int* imagePixels, int imageWidth);
	~PrimitivesCounter(void);

	// primitives1 has color as first key, primitives2 has length as first key.
	int countPrimitives(PrimitiveDirection direction, unordered_map <int, unordered_map <int, int>*> *primitives1,
		unordered_map <int, unordered_map <int, int>*> *primitives2, int xStart, int xEnd, int yStart, int yEnd);

	// Destroys the primitives table.
	void destroyPrimitives(unordered_map <int, unordered_map <int, int>*> *primitives);

private:	
	int* pixels;
	int imageWidth;
	
	// Counts horizontal primitives for one piece of an image
	int countPrimitivesHorizontal(unordered_map <int, unordered_map <int, int>*> *primitives1,
		unordered_map <int, unordered_map <int, int>*> *primitives2, int xStart, int xEnd, int yStart, int yEnd);

	int countPrimitivesVertical(unordered_map <int, unordered_map <int, int>*> *primitives1,
		unordered_map <int, unordered_map <int, int>*> *primitives2, int xStart, int xEnd, int yStart, int yEnd);

	int countPrimitivesBothDirections(unordered_map <int, unordered_map <int, int>*> *primitives1,
		unordered_map <int, unordered_map <int, int>*> *primitives2, int xStart, int xEnd, int yStart, int yEnd);

	void addPrimitive(unordered_map <int, unordered_map <int, int>*> *primitives, int key1, int key2);

	/* If the given key is not found in the first level map, then secon level map is added under it.
	If it already exists nothing happens. */
	void ensureMapLevel1(unordered_map <int, unordered_map <int, int>*> *level1Map, int level1Key);

	/* If the given key is not found in the second level map, 0 is added.
	After that primitives can be counted.
	If it already exists nothing happens. */
	void ensureMapLevel2(unordered_map <int, int> *level2Map, int level2Key);	
};

